﻿var tree;
var dataSource;
var splitter;
var splitterWidth = "330px";

jQuery(function ()
{
    //分栏
    if (typeof leftWidth != "undefined")
    {
        splitterWidth = leftWidth;
    }
    var height = $("body").height() - $("#toolbar").height();
    $("#lay-tree").height(height);
    splitter = $("#lay-tree").kendoSplitter(
    {
        panes:
        [
        { collapsible: true, size: splitterWidth },
        { collapsible: false }
        ]
    }).data("kendoSplitter");

    //树
    tree = $("#tree").data("kendoTreeView");
    dataSource = tree.dataSource;
});

function readTree(option)
{
    var modelTree = { children: "childs" };
    if (typeof (option.model) != 'undefined')
    {
        modelTree = option.model;
    }

    tree = $("#tree").kendoTreeView(
    {
        dataSource:
        {
            transport: { read: option.url },
            schema:
            {
                data: "Data",
                model: modelTree,
                errors: "Errors"
            },
            error: function (e)
            {
                alert(e.errors);
            }
        },
        dragAndDrop: true,
        dataBound: onDataBound,
        select: onSelect,
        dragend: onDragend
    }).data("kendoTreeView");
    dataSource = tree.dataSource;
}

function reload(data)
{
    //如果是重载
    if (!data)
    {
        dataSource.read();
        return;
    }

    //如果是删除
    if (!data.id)
    {
        var item = dataSource.get(data);
        var node = tree.findByUid(item.uid);
        tree.remove(node);

        //返回父级节点
        var parentItem = item.parentNode();
        if (parentItem)
        {
            nodeSelect(parentItem.id);
            return;
        }

        //返回同级第一节点
        var items = dataSource.data();
        if (items.length > 0)
        {
            nodeSelect(items[0].id);
            return;
        }
    }

    //如果是新增
    var item = dataSource.get(data.id);
    if (item == null)
    {
        var parentItem = dataSource.get(data.parentId);
        if (parentItem)
        {
            var parentNode = tree.findByUid(parentItem.uid);
            tree.append({ id: data.id, text: data.text }, parentNode);
        }
        else
        {
            tree.append({ id: data.id, text: data.text });
        }

        nodeSelect(data.id);
        return;
    }

    var parentItemOld = item.parentNode();
    var parentIdOld = parentItemOld == null ? null : parentItemOld.id;
    var node = tree.findByUid(item.uid);

    //如果是修改
    if (parentIdOld == data.parentId)
    {
        tree.text(node, data.text);
        nodeSelect(data.id);
        return;
    }

    //如果是修改并移动
    var parentItem = dataSource.get(data.parentId);
    tree.remove(node);
    if (parentItem)
    {
        var parentNode = tree.findByUid(parentItem.uid);
        tree.append({ id: data.id, text: data.text }, parentNode);
    }
    else
    {
        tree.append({ id: data.id, text: data.text });
    }

    nodeSelect(data.id);
}

function bind(result)
{
    var html = resultHelper.html(result);
    $("#detail").html(html);
}

function treeSelected()
{
    return tree.selectedId;
}

function treeSelectedItem() {
    return dataSource.get(tree.selectedId);
}

function nav(url)
{
    top.navTab(url);
}

function navNew(key, name, url) {
    top.activeTab(key, name, url);
}

function back()
{
    top.backTab()
}

function nodeSelect(id)
{
    var item = dataSource.get(id);
    var node = tree.findByUid(item.uid);
    tree.select(node);
    tree.selectedId = id;
    detail();
}

function onDataBound(e)
{
    if (e.node) return;
    var items = dataSource.data();
    if (items.length == 0) return;
    if (items.length == 1)
    {
        var node = tree.findByUid(items[0].uid);
        tree.expand(node);
    }
    nodeSelect(items[0].id);
}

var isRoot = true;

function onSelect(e)
{
    var item = dataSource.getByUid($(e.node).attr("data-uid"));
    nodeSelect(item.id);
}

function onDragend(e)
{
    var sourceItem = dataSource.getByUid($(e.sourceNode).attr("data-uid"));
    var destinationItem = dataSource.getByUid($(e.destinationNode).attr("data-uid"));
    move(sourceItem.id, destinationItem.id, e.dropPosition);
}